home *** CD-ROM | disk | FTP | other *** search
- #include "kant search.h"
- #include "kant text twiddling.h"
- #include "dialogs.h"
- #include "util.h"
- #include "prefs.h"
- #include "program globals.h"
-
- #define findDialog 400
-
- /* NUM_FIND is defined in program globals.h */
-
- Str255 gFindString[NUM_FIND];
- Str255 gReplaceString[NUM_FIND];
- short gLastFindPosition;
- short gWhichFound;
-
- static Boolean SearchDoneQQ(short *strPos, short *strLen);
-
- void SetLastFindPosition(short val)
- {
- gLastFindPosition=val;
- gWhichFound=-1;
- }
-
- short SearchForwards(TEHandle hTE, short *foundLength)
- {
- short i;
- short offset;
- short max;
- short strPos[NUM_FIND];
- short strLen[NUM_FIND];
- unsigned char *a;
-
- max=(**hTE).teLength;
- HLock((**hTE).hText);
- a=(unsigned char*)(*((**hTE).hText));
- for (i=0; i<NUM_FIND; i++)
- {
- strPos[i]=1;
- strLen[i]=gFindString[i][0];
- }
-
- offset=gLastFindPosition;
- while ((offset<max) && (!SearchDoneQQ(strPos, strLen)))
- {
- for (i=0; i<NUM_FIND; i++)
- {
- if (a[offset]==gFindString[i][strPos[i]])
- strPos[i]++;
- else
- strPos[i]=1;
- }
- offset++;
- }
-
- HUnlock((**hTE).hText);
- for (i=0; i<NUM_FIND; i++)
- {
- if ((strLen[i]>0) && (strPos[i]>strLen[i]))
- {
- gLastFindPosition=offset;
- *foundLength=strLen[i];
- return offset-strLen[i];
- }
- }
-
- return -1;
- }
-
- Boolean SearchDoneQQ(short *strPos, short *strLen)
- {
- Boolean done;
- short i;
-
- done=FALSE;
- for (i=0; i<NUM_FIND; i++)
- {
- if (gFindString[i][0]!=0x00)
- done|=(strPos[i]>strLen[i]);
- }
-
- return done;
- }
-
- #if 0
-
- short SearchBackwards(short startPosition, Str255 theStr, TEHandle hTE)
- /* note that startPosition is the starting position for looking for the last character in theStr */
- {
- short i;
- short strPos;
- short strLen;
- unsigned char *a;
-
- i=startPosition;
- HLock((**hTE).hText);
- a=(unsigned char*)(*((**hTE).hText));
- strLen=theStr[0];
- strPos=strLen;
- while ((i>=0) && (strPos>0))
- {
- if (a[i]==theStr[strPos])
- strPos--;
- else
- strPos=strLen;
- i--;
- }
-
- HUnlock((**hTE).hText);
- if (strPos==0)
- return i+1;
- else
- return -1;
- }
-
- #endif
-
- Boolean DoFindDialog(void)
- {
- DialogPtr theDlog;
- short itemSelected;
- Handle itemH;
- short itemType;
- Rect box;
- short i;
- Str255 theStr;
- ModalFilterUPP procFilter = NewModalFilterProc(TwoButtonFilter);
-
- PositionDialog('DLOG', findDialog);
- theDlog = GetNewDialog(findDialog, 0L, (WindowPtr)-1L);
-
- GetDItem(theDlog, 3, &itemType, &itemH, &box);
- InsetRect(&box, -4, -4);
- SetDItem(theDlog, 3, itemType, (Handle)OutlineDefaultButton, &box);
-
- for (i=0; i<NUM_FIND; i++)
- {
- GetDItem(theDlog, 6+i, &itemType, &itemH, &box);
- SetIText(itemH, gFindString[i]);
- GetDItem(theDlog, 10+i, &itemType, &itemH, &box);
- SetIText(itemH, gReplaceString[i]);
- }
-
- SetWTitle((WindowPtr)theDlog, "\pMulti-find");
- ShowWindow(theDlog);
- itemSelected=0;
- while ((itemSelected!=1) && (itemSelected!=2))
- {
- ModalDialog(procFilter, &itemSelected);
- }
-
- if (itemSelected==1)
- {
- for (i=0; i<NUM_FIND; i++)
- {
- GetDItem(theDlog, 6+i, &itemType, &itemH, &box);
- GetIText(itemH, theStr);
- Mymemcpy((Ptr)gFindString[i], (Ptr)theStr, theStr[0]+1);
- GetDItem(theDlog, 10+i, &itemType, &itemH, &box);
- GetIText(itemH, theStr);
- Mymemcpy((Ptr)gReplaceString[i], (Ptr)theStr, theStr[0]+1);
- }
- }
-
- HideWindow(theDlog);
- DisposeDialog(theDlog);
-
- if (itemSelected==1)
- {
- SaveThePrefs();
- gLastFindPosition=0;
- return DoFindAgain();
- }
-
- return FALSE;
- }
-
- Boolean DoFindAgain(void)
- {
- TEHandle hTE;
- short newOffset;
- short len;
-
- hTE=GetTheTextHandle();
- newOffset=SearchForwards(hTE, &len);
- if (newOffset!=-1)
- {
- TESetSelect(newOffset, newOffset+len, hTE);
- TESelView(hTE);
- return TRUE;
- }
-
- return FALSE;
- }
-
- Boolean AnythingToFindQQ(void)
- {
- return ((gFindString[0][0]!=0x00) || (gFindString[1][0]!=0x00) ||
- (gFindString[2][0]!=0x00) || (gFindString[3][0]!=0x00));
- }
-
- Boolean AnythingToReplaceQQ(void)
- {
- return ((gReplaceString[0][0]!=0x00) || (gReplaceString[1][0]!=0x00) ||
- (gReplaceString[2][0]!=0x00) || (gReplaceString[3][0]!=0x00));
- }
-